package com.example.shopping.gouwu.controller;


import com.example.shopping.JsonResult;
import com.example.shopping.gouwu.entity.*;
import com.example.shopping.gouwu.mapper.GouwuMapper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.connector.Request;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.runtime.Log;

import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
public class GouwuController {

    @Autowired
    GouwuMapper gouwuMapper;
    //查询主页轮播图
    @RequestMapping("/api/gouwu/homepage")
    public JsonResult getGoodsList() {
        List<Homepage> homepages = gouwuMapper.getMyHomepageList();
        return new JsonResult(true, "查询成功", homepages);
    }

    //查询所有商品
    @RequestMapping("/api/gouwu/goods")
    public JsonResult getGoodsList(int currPage,int pageSize) {
        PageHelper.startPage(currPage, pageSize);
        List<Goods> goods = gouwuMapper.getGoodsList();
        PageInfo<Goods> info=new PageInfo<Goods>(goods);
        return new JsonResult(true, "查询成功", info);
    }

    //模糊查询商品
    @RequestMapping("/api/gouwu/goodslike")
    public JsonResult getGoodsLike(String name,int currPage,int pageSize) throws Exception {
        PageHelper.startPage(currPage, pageSize);
        List<Goods> goods = gouwuMapper.getGoodsLike(name);
        PageInfo<Goods> info=new PageInfo<Goods>(goods);
        return new JsonResult(true, "查询成功", info);
    }

    //查询?条商品
    @RequestMapping("/api/gouwu/goodstk")
    public JsonResult getTkGoodsList(int tk) {
        List<Goods> goods = gouwuMapper.getTkGoodsList(tk);
        return new JsonResult(true, "查询成功", goods);
    }

    //分页查询商品
    @RequestMapping("/api/gouwu/fygoods")
    public JsonResult getFyGoodsList(int currPage,int pageSize) {
        List<Goods> goods = gouwuMapper.getFyGoodsList(currPage,pageSize);
        int i=gouwuMapper.getMyGoodsList();
        Mywww mywww=new Mywww();
        mywww.setTotal(i);
        mywww.setGoods(goods);
        return new JsonResult(true, "查询成功", mywww);
    }

    //通过id查询商品
    @RequestMapping("/api/gouwu/goodsid")
    public JsonResult getIDgoodsList(int id) {
        Goods goods = gouwuMapper.getIDgoodsList(id);
        return new JsonResult(true, "查询成功", goods);
    }

    //使用商品大类的id查询该大类的商品
    @RequestMapping("/api/gouwu/productidGoodgs")
    public JsonResult getDlGoodsList(int productid,int currPage,int pageSize) {
        PageHelper.startPage(currPage, pageSize);
        List<Goods> goods = gouwuMapper.getDlGoods(productid);
        PageInfo<Goods> info=new PageInfo<Goods>(goods);
        return new JsonResult(true, "查询成功", info);
    }

    //使用商品大类的id查询该大类的商品
    @RequestMapping("/api/gouwu/prgoodgslist")
    public JsonResult getProductgoodsList(int productid,int currPage,int pageSize) {
        PageHelper.startPage(currPage, pageSize);
        List<Goods> goods = gouwuMapper.getDlGoods(productid);
        PageInfo<Goods> info=new PageInfo<Goods>(goods);
        return new JsonResult(true, "查询成功", info);
    }

    //添加商品
    @RequestMapping("/api/gouwu/add")
    public JsonResult addAdmin(Goods goods) {
        int i = gouwuMapper.AddGoods(goods);
        JsonResult jsonResult = new JsonResult(true, "添加成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "添加失败", null);
        }
    }
    //查询多少条数据
    @RequestMapping("/api/gouwu/getGoodsdescList")
    public JsonResult getGoodsdescList() {
        Goods goods = gouwuMapper.getGoodsdescList();
        System.out.println(goods.getId());
        return new JsonResult(true, "查询成功", goods);
    }

    //删除商品
    @RequestMapping("/api/guowu/dele")
    public JsonResult DeleGoods(int id) {
        int i = gouwuMapper.DeleGoods(id);
        JsonResult jsonResult = new JsonResult(true, "删除成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "删除失败", null);
        }
    }

    //修改商品
        @RequestMapping("/api/goods/up")
    public JsonResult UpGoods(Goods goods) {
        //System.out.println(goods);
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = ft.format(date);
        goods.setUpdated(time);
        //System.out.println(goods);
        int i = gouwuMapper.UpGoods(goods);
        JsonResult jsonResult = new JsonResult(true, "修改成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "修改失败", null);
        }
    }

    //修改主图
    @RequestMapping("/api/goods/upiconurl")
    public JsonResult UpGoodsiconurl(Goods goods) {
        //System.out.println(goods);
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = ft.format(date);
        goods.setUpdated(time);
        //System.out.println(goods);
        int i = gouwuMapper.UpGoodsiconurl(goods);
        JsonResult jsonResult = new JsonResult(true, "修改成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "修改失败", null);
        }
    }

    //查询所有商品大类
    @RequestMapping("/api/gouwu/Category")
    public JsonResult getCategoryList() {
        List<Category> category = gouwuMapper.getCategoryList();
        return new JsonResult(true, "查询成功", category);
    }

    //修改商品大类状态
    @RequestMapping("/api/cstatus/up")
    public JsonResult UpCstatus(int cstatus,int id) {
        int i = gouwuMapper.UpCstatus(cstatus,id);
        JsonResult jsonResult = new JsonResult(true, "修改状态成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "修改状态失败", null);
        }
    }

    //添加商品大类
    @RequestMapping("/api/category/add")
    public JsonResult AddCategory(Category category) {
        int i = gouwuMapper.AddCategory(category);
        JsonResult jsonResult = new JsonResult(true, "添加成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "添加失败", null);
        }
    }

    //删除商品大类
    @RequestMapping("/api/category/dele")
    public JsonResult DeleCategory(int id) {
        int i = gouwuMapper.DeleCategory(id);
        JsonResult jsonResult = new JsonResult(true, "删除成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "删除失败", null);
        }
    }

    //用id查询商品大类
    @RequestMapping("/api/gouwu/idcategory")
    public JsonResult getCategoryIDList(int id) {
        Category category = gouwuMapper.getCategoryIDList(id);
        return new JsonResult(true, "查询成功", category);
    }

    //修改商品大类
    @RequestMapping("/api/category/up")
    public JsonResult UpCategory(Category category) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = ft.format(date);
        category.setUpdated(time);
        int i = gouwuMapper.UpCategory(category);
        JsonResult jsonResult = new JsonResult(true, "修改成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "修改失败", null);
        }
    }

    //
    @RequestMapping("/api/category/upiconurl")
    public JsonResult UpCategoryiconurl(Category category) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = ft.format(date);
        category.setUpdated(time);
        int i = gouwuMapper.UpCategoryiconurl(category);
        JsonResult jsonResult = new JsonResult(true, "修改成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "修改失败", null);
        }
    }

    //图片上传
    @PostMapping("/nginx/uploadByAction")
    public String uploadByNginx(@RequestPart("file") MultipartFile file) {
        System.out.println(file);
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        String fileName = file.getOriginalFilename();
        File dest = new File("C:\\img\\" + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
    }

    //删除商品轮播图
    @RequestMapping("/api/goodsimage/dele")
    public JsonResult DeleGoodsimage(int goodsid) {
        int i = gouwuMapper.DeleGoodsimage(goodsid);
        JsonResult jsonResult = new JsonResult(true, "删除成功", i);
        //System.out.println(i);
        if (i != 0) {
            return jsonResult;
        } else {
            return new JsonResult(false, "删除失败", null);
        }
    }
    //用商品id查询商品轮播图
    @RequestMapping("/api/gouwu/goodsimages")
    public JsonResult getGoodsimageList(int goodsid) {
        List<Goodsimage> goodsimages = gouwuMapper.getGoodsimageList(goodsid);
        return new JsonResult(true, "查询成功", goodsimages);
    }

    //添加商品轮播图
    @RequestMapping("/api/goodsimage/add")
    public JsonResult AddGoodsimage(Goodsimage goodsimage) {
        Goods goods = gouwuMapper.getGoodsdescList();
        goodsimage.setGoodsid(goods.getId());
        int i = gouwuMapper.AddGoodsimage(goodsimage);
        JsonResult jsonResult = new JsonResult(true, "添加成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "添加失败", null);
        }
    }

    //修改商品轮播图
    @RequestMapping("/api/goodsimage/up")
    public JsonResult UpGoodsimage(Goodsimage goodsimage) {
        int i = gouwuMapper.AddGoodsimage(goodsimage);
        JsonResult jsonResult = new JsonResult(true, "修改成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "修改失败", null);
        }
    }

    //收藏查询
    @RequestMapping("/api/gouwu/getcollectionlist")
    public JsonResult getCollectionList(int currPage,int pageSize) {
        PageHelper.startPage(currPage, pageSize);
        List<Collection> collections = gouwuMapper.getCollectionList();
        PageInfo<Collection> info=new PageInfo<Collection>(collections);
        return new JsonResult(true, "查询成功", info);
    }

    //用户查询收藏
    @RequestMapping("/api/gouwu/getcollectionid")
    public JsonResult getCollectionIdList(int userid) {
        List<Collection> collections = gouwuMapper.getCollectionIdList(userid);
        return new JsonResult(true, "查询成功", collections);
    }

    //添加收藏
    @RequestMapping("/api/Collection/add")
    public JsonResult AddCollection(Collection collection) {
        int i = gouwuMapper.AddCollection(collection);
        JsonResult jsonResult = new JsonResult(true, "添加成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "添加失败", null);
        }
    }

    //删除收藏
    @RequestMapping("/api/Collection/dele")
    public JsonResult DeleCollection(int id) {
        int i = gouwuMapper.DeleCollection(id);
        JsonResult jsonResult = new JsonResult(true, "删除成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "删除失败", null);
        }
    }

}
